package com.cdks.wuxiu.api.service.mysql;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.mapper.VideoShowInfoMapper;
import com.cdks.wuxiu.entity.mysql.VideoShowInfo;

import java.util.List;

import org.springframework.stereotype.Service;

/**
 * 用户视频 服务实现类
 */
@Service
public class VideoShowInfoServiceImpl extends ServiceImpl<VideoShowInfoMapper, VideoShowInfo> implements VideoShowInfoService {
    @Override
    public int deleteByUser(Long userId) {
        QueryWrapper<VideoShowInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(VideoShowInfo::getUserId, userId);
        baseMapper.delete(wrapper);
        
        return 0;
    }

    @Override
    public int deleteByName(Long userId, String videoUri) {
        QueryWrapper<VideoShowInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(VideoShowInfo::getVideoUri, videoUri)
                        .eq(VideoShowInfo::getUserId, userId);
        baseMapper.delete(wrapper);
        
        return 0;
    }

    @Override
    public List<VideoShowInfo> getVideosByUser(Long userId, Integer page) {
        QueryWrapper<VideoShowInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(VideoShowInfo::getUserId, userId)
                        .eq(VideoShowInfo::getStatus, Constant.AUDIT_STATUS_PASS)
                        .select(VideoShowInfo::getId,
                                VideoShowInfo::getImageUri,
                                VideoShowInfo::getVideoUri,
                                VideoShowInfo::getPlayCnt,
                                VideoShowInfo::getRewardCnt)
                        .orderByDesc(VideoShowInfo::getId);

        return baseMapper.selectList(wrapper.last("LIMIT " + page * Constant.LIST_PAGE_SIZE + "," + Constant.LIST_PAGE_SIZE));
    }

    @Override
    public List<VideoShowInfo> getPlayList(long selfId, int sex) {
        if (selfId == 0)
        {
            QueryWrapper<VideoShowInfo> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(VideoShowInfo::getStatus, Constant.AUDIT_STATUS_PASS)
                            .select(VideoShowInfo::getId,
                                    VideoShowInfo::getUserId,
                                    VideoShowInfo::getImageUri,
                                    VideoShowInfo::getVideoUri,
                                    VideoShowInfo::getPlayCnt,
                                    VideoShowInfo::getRewardCnt)
                            .orderByDesc(VideoShowInfo::getId);

            return baseMapper.selectList(wrapper.last("LIMIT 5"));
        }

        List<VideoShowInfo> res  = baseMapper.getPlayList(selfId, sex);
        if (res.isEmpty())
        {
            return baseMapper.getRandList(selfId, sex);
        }

        return res;
    }

    @Override
    public int addPlayCount(Long videoId) {
        return baseMapper.addPlayCount(videoId);
    }

    @Override
    public int addRewardCount(Long videoId) {
        return baseMapper.addRewardCount(videoId);
    }    
}
